/**
 * Created by LeungZ on 2016/10/21.
 */
class Drct {
    constructor() {
        this.replace = true;
        this.transclude = true;
        this.scope = {
            "onUpload": '&'
        };
        this.restrict = 'E';
        this.template = '<button type="button"><ng-transclude></ng-transclude><input type="file" style="display:none"></button>';
    }

    link(scope, element, attr) {
        let filele = element.find('input');

        if (attr.hasOwnProperty('multiple')) {
            filele.attr('multiple', true);
        }
        if (attr.accept) {
            filele.attr('accept', attr.accept);
        }
        element.bind('click', function (e) {
            e.preventDefault();
            filele.click()
        });
        filele.bind('click', function (e) {
            e.stopPropagation();
        });
        filele.bind('change', function (event) {
            scope.$evalAsync(function () {
                scope.onUpload({"files": event.target.files});
            });
        });
    }

    static factory() {
        return new Drct();
    }
}
export default Drct;